package com.tingyuge.base.jdk.jdk8;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * @Author zhangqp
 * @Description  list 去重
 */
@AllArgsConstructor
@Data
@NoArgsConstructor
public class UserTest {
    private String userid;
    private String username;
    private String age;
    private String address;

    public static void main(String[] args) {
        List<UserTest> userList = new ArrayList<>();

        userList.add(new UserTest("1","李大锤","23","南京"));
        userList.add(new UserTest("2","张无忌","18","西安"));
        userList.add(new UserTest("3","刘德华","26","苏州"));
        userList.add(new UserTest("4","郭靖","33","上海"));

        userList.add(new UserTest("1","李大锤","23","南京"));    //id相同，其他数据也相同
        userList.add(new UserTest("3","带头大哥","36","杭州"));  //id相同，其他数据不同

        System.out.println(userList);
        //根据userid去重
        userList = userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(UserTest :: getUserid))), ArrayList::new));
        System.out.println(userList);
    }
}
